﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Doo.DataView;
using Doo.WebUI;

namespace Doo.Controls
{
    /// <summary>
    /// 样式组（TODO）
    /// </summary>
    public class GridViewCssGroup
    {
        /// <summary>
        /// 奇数行样式
        /// </summary>
        public string OddRowCss { get; set; }
    }

    [ParseChildren(true)]
    public class GridView : DataView.DataViewBase, Doo.Scripts.IClientEvent<GridView.GridViewClientEvents>
    {
        const string _ScriptName = "Doo.WebUI.DataView.GridView.script.js";

        #region 基础属性
        public override string ClientControlType
        {
            get
            {
                return "Doo.GridView";
            }
        }

        public override string ScriptResourceName
        {
            get
            {
                return _ScriptName;
            }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected override string TagName
        {
            get
            {
                return "div";
            }
        }
        #endregion

        #region 客户端事件

        public class GridViewClientEvents : DataViewClientEvents
        {
            [ClientEvent("rowmoving")]
            public Doo.Scripts.ClientEvent RowMoving { get; set; }

            [ClientEvent("rowmoved")]
            public Doo.Scripts.ClientEvent RowMoved { get; set; }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public GridViewClientEvents ClientEvents { get; set; }

        #endregion

        /// <summary>
        /// 空数据模板
        /// </summary>
        public string EmptyTemplate { get; set; }

        /// <summary>
        /// 是否使用列页脚
        /// </summary>
        public bool ShowFieldFooter { get; set; }

        /// <summary>
        /// 是否使用行编号
        /// </summary>
        public bool ShowRowIndex { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (this.TemplateEvents != null) this.TemplateEvents.Init(Page, Parent);
            if (this.ClientEvents != null) this.ClientEvents.Init(Page, Parent);
        }

        /// <summary>
        /// 数据字段
        /// </summary>
        [ReadOnly(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual List<DataField> Columns { get; set; }

        private string _cssclass;
        public override string CssClass
        {
            get
            {
                return _cssclass;
            }
            set
            {
                _cssclass = value;
            }
        }

        /// <summary>
        /// 是否要显示表头
        /// </summary>
        public bool ShowHeader { get; set; }

        /// <summary>
        /// 表格的Caption
        /// </summary>
        public string Caption { get; set; }

        protected override void SetScriptDescriptor(ScriptControlDescriptor descriptor)
        {
            base.SetScriptDescriptor(descriptor);

            if (this.TemplateEvents != null && this.TemplateEvents.Count > 0)
            {
                descriptor.AddProperty("tplevents", this.TemplateEvents.ToHashtable());
            }

            if (!string.IsNullOrEmpty(_cssclass))
            {
                descriptor.AddProperty("css", _cssclass);
            }

            if (!string.IsNullOrEmpty(EmptyTemplate))
            {
                descriptor.AddProperty("emptytpl", EmptyTemplate);
            }

            if (this.ShowRowIndex)
            {
                descriptor.AddProperty("showRowIndex", ShowRowIndex);
            }

            if (this.ShowFieldFooter)
            {
                descriptor.AddProperty("showfooter", this.ShowFieldFooter);
            }

            if (this.Columns != null && this.Columns.Count > 0)
            {
                List<string[]> fs = new List<string[]>();
                foreach (var field in this.Columns)
                {
                    fs.Add(new string[] { field.HeaderText, field.ToTemplate(), field.ToHeader(), field.CssClass, field.FooterText });
                }
                descriptor.AddProperty("columns", fs);
            }
        }

    }
}
